package com.example.demo.tx.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.util.HttpUtil;
import com.example.demo.util.ResultUtil;
import org.springframework.stereotype.Service;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Map;

/**
 * @Author: zjq
 * @Date: create in 2023/6/15 15:47
 */
@Service
public class TxServiceImpl implements TxService {

    private static final String token = "ihaierForTodoKey";
    private static final String key = "5bfffbf26960f5c3b9aaec7bf81ae310";

    /**
     * 验证微信签名
     */
    @Override
    public boolean checkSignature(String signature, String timestamp, String nonce) {
        // 1.将token、timestamp、nonce三个参数进行字典序排序
        String[] arr = new String[]{token, timestamp, nonce};
        Arrays.sort(arr);
        // 2. 将三个参数字符串拼接成一个字符串进行sha1加密
        StringBuilder content = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            content.append(arr[i]);
        }
        MessageDigest md = null;
        String tmpStr = null;
        try {
            md = MessageDigest.getInstance("SHA-1");
            // 将三个参数字符串拼接成一个字符串进行sha1加密
            byte[] digest = md.digest(content.toString().getBytes());
            tmpStr = byteToStr(digest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        content = null;
        // 3.将sha1加密后的字符串可与signature对比，标识该请求来源于微信
        return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;
    }

    @Override
    public Object getweather(String adcode, String extensions) {
        if (adcode == null) {
            return ResultUtil.info(-1, "城市的adcode不能为null");
        }
        String url = "https://restapi.amap.com/v3/weather/weatherInfo?key=" + key;
        url = url + "&city=" + adcode;

        if (extensions != null) {
            url = url + "&extensions=" + extensions;
        }

        String obj = HttpUtil.get(url);
        JSONObject json = JSON.parseObject(obj);
        if (json.get("infocode").equals("10000")) {
            return ResultUtil.success("data", json.get("lives"));
        } else {
            return ResultUtil.info(-1, obj);
        }

    }

    @Override
    public Object getIp() {
        String url = "https://restapi.amap.com/v3/ip?key=" + key;
        return HttpUtil.get(url);
        //String obj = HttpUtil.get(url);
        //Map<String, Object> map = JSON.parseObject(obj, Map.class);
        //if (map.get("status").equals("1")) {
        //    return ResultUtil.success("data", map);
        //} else {
        //    return map;
        //}

    }


    private static String byteToStr(byte[] byteArray) {
        StringBuilder strDigest = new StringBuilder();
        for (int i = 0; i < byteArray.length; i++) {
            strDigest.append(byteToHexStr(byteArray[i]));
        }
        return strDigest.toString();
    }

    private static String byteToHexStr(byte mByte) {
        char[] Digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
                'B', 'C', 'D', 'E', 'F'};
        char[] tempArr = new char[2];
        tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
        tempArr[1] = Digit[mByte & 0X0F];
        String s = new String(tempArr);
        return s;
    }
}
